package cn.com.css.taxInspection.attention.service.impl;

import cn.com.css.taxInspection.attention.mapper.JcAjglSdbbMapper;
import cn.com.css.taxInspection.attention.service.JcAjglSdbbService;
import cn.com.css.taxInspection.gt3.entity.JcAjglSdbbEntity;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
@Slf4j
public class JcAjglSdbbServiceImpl extends ServiceImpl<JcAjglSdbbMapper, JcAjglSdbbEntity> implements JcAjglSdbbService {

    @Resource
    private HttpSessionService httpSessionService;

    @Resource
    private JcAjglSdbbService jcAjglSdbbService;

    //查询案件维护列表
    @Override
    @DS("oracle")
    public IPage<JcAjglSdbbEntity> getJcAjglSdbbPage(JcAjglSdbbEntity jcAjglSdbbEntity) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        IPage<JcAjglSdbbEntity> page = new Page<>(jcAjglSdbbEntity.getPage(), jcAjglSdbbEntity.getLimit());
        LambdaQueryWrapper<JcAjglSdbbEntity> queryWrapper = Wrappers.lambdaQuery();
        // 暂时只查本局的案件
        if ("qs".equals(jcAjglSdbbEntity.getCxbz())) {
            queryWrapper.like(JcAjglSdbbEntity::getSwjgdm, httpSessionService.getCurrentDeptNo().substring(0, 8));
        } else {
            queryWrapper.eq(JcAjglSdbbEntity::getSwjgdm, httpSessionService.getCurrentDeptNo().substring(0, 9) + "00");
        }
        // 案件编号
        if (TsBqUtils.isNotNull(jcAjglSdbbEntity.getJcajbh())) {
            queryWrapper.like(JcAjglSdbbEntity::getJcajbh, jcAjglSdbbEntity.getJcajbh());
        }
        // 纳税人识别号
        if (TsBqUtils.isNotNull(jcAjglSdbbEntity.getNsrsbh())) {
            queryWrapper.like(JcAjglSdbbEntity::getNsrsbh, jcAjglSdbbEntity.getNsrsbh());
        }
        // 纳税人名称
        if (TsBqUtils.isNotNull(jcAjglSdbbEntity.getNsrmc())) {
            queryWrapper.like(JcAjglSdbbEntity::getNsrmc, jcAjglSdbbEntity.getNsrmc());
        }
        // 案源下发时间
        if (!StringUtils.isEmpty(jcAjglSdbbEntity.getStartTime())) {
            queryWrapper.ge(JcAjglSdbbEntity::getAyxfsj, sdf.parse(jcAjglSdbbEntity.getStartTime()));
            queryWrapper.le(JcAjglSdbbEntity::getAyxfsj, sdf.parse(jcAjglSdbbEntity.getEndTime()));
        }
        // 案件检查状态
        if (TsBqUtils.isNotNull(jcAjglSdbbEntity.getAjjczt())) {
            queryWrapper.like(JcAjglSdbbEntity::getAjjczt, jcAjglSdbbEntity.getAjjczt());
        }
        // 根据案源下发时间倒序
        queryWrapper.orderByDesc(JcAjglSdbbEntity::getAyxfsj);
        return this.page(page, queryWrapper);
    }

    @Override
    @DS("oracle")
    public List<JcAjglSdbbEntity> exportJcAjglSdbbPage(JcAjglSdbbEntity jcAjglSdbbEntity) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        LambdaQueryWrapper<JcAjglSdbbEntity> queryWrapper = Wrappers.lambdaQuery();
        // 暂时只查本局的案件
        if ("qs".equals(jcAjglSdbbEntity.getCxbz())) {
            queryWrapper.like(JcAjglSdbbEntity::getSwjgdm, httpSessionService.getCurrentDeptNo().substring(0, 8));
        } else {
            queryWrapper.eq(JcAjglSdbbEntity::getSwjgdm, httpSessionService.getCurrentDeptNo().substring(0, 9) + "00");
        }
        // 案件编号
        if (TsBqUtils.isNotNull(jcAjglSdbbEntity.getJcajbh())) {
            queryWrapper.like(JcAjglSdbbEntity::getJcajbh, jcAjglSdbbEntity.getJcajbh());
        }
        // 纳税人识别号
        if (TsBqUtils.isNotNull(jcAjglSdbbEntity.getNsrsbh())) {
            queryWrapper.like(JcAjglSdbbEntity::getNsrsbh, jcAjglSdbbEntity.getNsrsbh());
        }
        // 纳税人名称
        if (TsBqUtils.isNotNull(jcAjglSdbbEntity.getNsrmc())) {
            queryWrapper.like(JcAjglSdbbEntity::getNsrmc, jcAjglSdbbEntity.getNsrmc());
        }
        // 案源下发时间
        if (!StringUtils.isEmpty(jcAjglSdbbEntity.getStartTime())) {
            queryWrapper.ge(JcAjglSdbbEntity::getAyxfsj, sdf.parse(jcAjglSdbbEntity.getStartTime()));
            queryWrapper.le(JcAjglSdbbEntity::getAyxfsj, sdf.parse(jcAjglSdbbEntity.getEndTime()));
        }
        // 案件检查状态
        if (TsBqUtils.isNotNull(jcAjglSdbbEntity.getAjjczt())) {
            queryWrapper.like(JcAjglSdbbEntity::getAjjczt, jcAjglSdbbEntity.getAjjczt());
        }
        // 根据案源下发时间倒序
        queryWrapper.orderByDesc(JcAjglSdbbEntity::getAyxfsj);
        return jcAjglSdbbService.list(queryWrapper);
    }
}
